#jinja2: trim_blocks:True,lstrip_blocks:True
---

scope: {{ patroni_cluster_name }}
name: {{ ansible_hostname }}
namespace: /{{ patroni_etcd_namespace | default('service') }}

log:
  level: {{ patroni_log_level |upper }}
  traceback_level: {{ patroni_log_traceback_level |upper }}
  format: {{ patroni_log_format |quote }}
  dateformat: {{ patroni_log_dateformat |quote }}
  max_queue_size: {{ patroni_log_max_queue_size |int }}
  {% if patroni_log_destination == 'logfile' %}
  dir: {{ patroni_log_dir }}
  file_num: {{ patroni_log_file_num |int }}
  file_size: {{ patroni_log_file_size |int }}
  {% endif %}
  loggers:
    patroni.postmaster: {{ patroni_log_loggers_patroni_postmaster |upper }}
    urllib3: {{ patroni_log_loggers_urllib3 |upper }}

restapi:
  listen: {{ patroni_restapi_listen_addr }}:{{ patroni_restapi_port }}
  connect_address: {{ patroni_restapi_connect_addr | default(patroni_bind_address | default(bind_address, true), true) }}:{{ patroni_restapi_port }}
  {% if patroni_restapi_protocol | default('http') == 'https' and (patroni_restapi_certfile | default('') | length > 0 and patroni_restapi_keyfile | default('') | length > 0) %}
  certfile: {{ patroni_restapi_certfile }}
  keyfile: {{ patroni_restapi_keyfile }}
  {% endif %}
  {% if patroni_restapi_protocol | default('http') == 'https' and patroni_restapi_cafile | default('') | length > 0 %}
  cafile: {{ patroni_restapi_cafile }}
  {% endif %}
{% if patroni_restapi_password | default('') | length > 0 %}
  authentication:
    username: {{ patroni_restapi_username | default('patroni') }}
    password: {{ patroni_restapi_password }}
{% else %}
#  authentication:
#    username: username
#    password: password
{% endif %}
{% if patroni_restapi_request_queue_size is defined %}
  request_queue_size: {{ patroni_restapi_request_queue_size |int }}
{% endif %}

{% if dcs_type == 'etcd' %}
etcd3:
  {% if not dcs_exists|bool %}
  hosts: {% for host in etcd_hosts %}{{ hostvars[host]['etcd_bind_address'] | default(hostvars[host]['bind_address'], true) }}:2379{% if not loop.last %},{% endif %}{% endfor %}
  {% endif %}
  {% if dcs_exists|bool %}
  hosts: {% for etcd_hosts in patroni_etcd_hosts %}{{etcd_hosts.host}}:{{etcd_hosts.port}}{% if not loop.last %},{% endif %}{% endfor %}
  {% endif %}

  {% if etcd_tls_enable | default(false) | bool %}
  protocol: https
  cacert: {{ patroni_etcd_cacert | default('/etc/patroni/tls/etcd/ca.crt') }}
  cert: {{ patroni_etcd_cert | default('/etc/patroni/tls/etcd/server.crt') }}
  key: {{ patroni_etcd_key | default('/etc/patroni/tls/etcd/server.key') }}
  {% endif %}
  {% if patroni_etcd_username | default('') | length > 0 %}
  username: {{ patroni_etcd_username | default('') }}
  {% endif %}
  {% if patroni_etcd_password | default('') | length > 0 %}
  password: {{ patroni_etcd_password }}
  {% endif %}
{% endif %}

{% if dcs_type == 'consul' %}
consul:
  host: 127.0.0.1:8500
  {% if consul_tls_enable | default(false) | bool %}
  scheme: https
  cacert: {{ patroni_consul_cacert | default('/etc/patroni/tls/consul/ca.crt') }}
  cert: {{ patroni_consul_cert | default('/etc/patroni/tls/consul/server.crt') }}
  key: {{ patroni_consul_key |  default('/etc/patroni/tls/consul/server.key') }}
  {% endif %}
{% endif %}

bootstrap:
  method: {{ patroni_cluster_bootstrap_method }}
{% if patroni_cluster_bootstrap_method == 'wal-g' %}
  wal-g:
    command: {{ wal_g_patroni_cluster_bootstrap_command }}
    no_params: True
    recovery_conf:
      {% for item in wal_g_patroni_cluster_bootstrap_recovery_conf %}
      {% for key, value in item.items() %}
      {{ key }}: {{ value }}
      {% endfor %}
      {% endfor %}
{% endif %}
{% if patroni_cluster_bootstrap_method == 'pg_probackup' %}
  pg_probackup:
    command: {{ pg_probackup_patroni_cluster_bootstrap_command }}
    no_params: true
{% endif %}
  dcs:
    ttl: {{ patroni_ttl |d(30, true) |int }}
    loop_wait: {{ patroni_loop_wait |d(10, true) |int }}
    retry_timeout: {{ patroni_retry_timeout |d(10, true) |int }}
    maximum_lag_on_failover: {{ patroni_maximum_lag_on_failover | d(1048576) | int }}
    master_start_timeout: {{ patroni_master_start_timeout |d(300, true) |int }}
    synchronous_mode: {{ synchronous_mode |string |d(false, true) |lower }}
    synchronous_mode_strict: {{ synchronous_mode_strict |string |d(false, true) |lower }}
    synchronous_node_count: {{ synchronous_node_count |d(1, true) |int }}
    postgresql:
      use_pg_rewind: {{ patroni_postgresql_use_pg_rewind |string |d(false, true) |lower }}
      use_slots: true
      parameters:
      {% for parameter in postgresql_parameters %}
        {{ parameter.option }}: "{{ parameter.value }}"
      {% endfor %}
    {% if patroni_standby_cluster.host is defined and patroni_standby_cluster.host | length > 0 %}
    standby_cluster:
      host: {{ patroni_standby_cluster.host }}
      port: {{ patroni_standby_cluster.port }}
      {% if patroni_standby_cluster.primary_slot_name is defined and patroni_standby_cluster.primary_slot_name | length > 0 %}
      primary_slot_name: {{ patroni_standby_cluster.primary_slot_name }}
      {% endif %}
      {% if patroni_standby_cluster.restore_command is defined and patroni_standby_cluster.restore_command | length > 0 %}
      restore_command: {{ patroni_standby_cluster.restore_command }}
      {% endif %}
      {% if patroni_standby_cluster.recovery_min_apply_delay is defined and patroni_standby_cluster.recovery_min_apply_delay | length > 0 %}
      recovery_min_apply_delay: {{ patroni_standby_cluster.recovery_min_apply_delay }}
     {% endif %}
    {% endif %}
    {% if patroni_slots is defined and patroni_slots | length > 0 %}
    slots:
      {% for slot in patroni_slots %}
      {{ slot.slot }}:
        type: {{ slot.type }}
        {% if slot.plugin | default('') | length > 0 %}
        plugin: {{ slot.plugin }}
        {% endif %}
        {% if slot.database | default('') | length > 0 %}
        database: {{ slot.database }}
        {% endif %}
      {% endfor %}
    {% endif %}

  initdb:  # List options to be passed on to initdb
    - encoding: {{ postgresql_encoding }}
    - locale: {{ postgresql_locale }}
  {% if postgresql_data_checksums|bool %}
    - data-checksums
  {% endif %}

  pg_hba:  # Add following lines to pg_hba.conf after running 'initdb'
    - host replication {{ patroni_replication_username }} 127.0.0.1/32 {{ postgresql_password_encryption_algorithm }}
    - host all all 0.0.0.0/0 {{ postgresql_password_encryption_algorithm }}

postgresql:
  listen: {{ postgresql_listen_addr }}:{{ postgresql_port }}
  connect_address: {{ postgresql_connect_addr | default(patroni_bind_address | default(bind_address, true), true) }}:{{ postgresql_port }}
{% if patroni_use_unix_socket | default(false) %}
  use_unix_socket: true
{% endif %}
{% if patroni_use_unix_socket_repl | default(false) %}
  use_unix_socket_repl: true
{% endif %}
  data_dir: {{ postgresql_data_dir }}
  bin_dir: {{ postgresql_bin_dir }}
  config_dir: {{ postgresql_conf_dir }}
  pgpass: {{ postgresql_home_dir }}/.pgpass_patroni
  authentication:
    replication:
      username: {{ patroni_replication_username }}
      password: {{ patroni_replication_password }}
{% for parameter in patroni_replication_auth_options %}
{%   if parameter.option == 'sslrootcert' and parameter.value | length > 0 %}
      {{ parameter.option }}: {{ parameter.value }}
{%   elif parameter.option != 'sslrootcert' %}
      {{ parameter.option }}: {{ parameter.value }}
{%   endif %}
{% endfor %}
    superuser:
      username: {{ patroni_superuser_username }}
      password: {{ patroni_superuser_password }}
{% for parameter in patroni_superuser_auth_options %}
{%   if parameter.option == 'sslrootcert' and parameter.value | length > 0 %}
      {{ parameter.option }}: {{ parameter.value }}
{%   elif parameter.option != 'sslrootcert' %}
      {{ parameter.option }}: {{ parameter.value }}
{%   endif %}
{% endfor %}
#    rewind:  # Has no effect on postgres 10 and lower
#      username: rewind_user
#      password: rewind_password
  parameters:
    unix_socket_directories: {{ postgresql_unix_socket_dir }}
{% if local_postgresql_parameters is defined and local_postgresql_parameters | length > 0 %}
  {% for parameter in local_postgresql_parameters %}
    {{ parameter.option }}: "{{ parameter.value }}"
  {% endfor %}
{% endif %}
{% if postgresql_stats_temp_directory_path is defined and postgresql_stats_temp_directory_path != 'none' and postgresql_version | int <= 14 %}
    stats_temp_directory: {{ postgresql_stats_temp_directory_path }}
{% endif %}

{% if postgresql_pg_ident is defined and postgresql_pg_ident | length > 0 %}
  pg_ident:
  {% for ident in postgresql_pg_ident %}
    - {{ ident.mapname }} {{ ident.system_username }} {{ ident.pg_username }}
  {% endfor %}
{% endif %}

  remove_data_directory_on_rewind_failure: {{ patroni_remove_data_directory_on_rewind_failure |string |d(false, true) |lower }}
  remove_data_directory_on_diverged_timelines: {{ patroni_remove_data_directory_on_diverged_timelines |string |d(false, true) |lower }}

{% if patroni_callbacks is defined and patroni_callbacks | length > 0 %}
  callbacks:
  {% for callback in patroni_callbacks %}
    {% if callback.script | length > 0 %}
      {{ callback.action }}: '{{ callback.script }}'
    {% endif %}
  {% endfor %}
{% endif %}

  create_replica_methods:
{% if patroni_create_replica_methods is defined and patroni_create_replica_methods | length > 0 %}
  {% for create_replica_method in patroni_create_replica_methods %}
    - {{ create_replica_method }}
  {% endfor %}
  {% if 'pgbackrest' in patroni_create_replica_methods %}
  pgbackrest:
    {% for item in pgbackrest %}
    {{ item.option }}: {{ item.value }}
    {% endfor %}
  {% endif %}
  {% if 'wal_g' in patroni_create_replica_methods %}
  wal_g:
    {% for item in wal_g %}
    {{ item.option }}: {{ item.value }}
    {% endfor %}
  {% endif %}
  {% if 'basebackup' in patroni_create_replica_methods %}
  basebackup:
    {% for item in basebackup %}
    {{ item.option }}: '{{ item.value }}'
    {% endfor %}
  {% endif %}
  {% if 'pg_probackup' in patroni_create_replica_methods %}
  pg_probackup:
    {% for item in pg_probackup %}
    {{ item.option }}: {{ item.value }}
    {% endfor %}
  {% endif %}
{% else %}
    - basebackup
  basebackup:
    max-rate: '100M'
{% endif %}

{% if postgresql_restore_command is defined and postgresql_restore_command | length > 0 %}
  recovery_conf:
    restore_command: {{ postgresql_restore_command }}
{% endif %}

watchdog:
  mode: {{ patroni_watchdog_mode if patroni_watchdog_mode in ['off', 'automatic', 'required'] else 'off'}}  # Allowed values: off, automatic, required
  device: {{ patroni_watchdog_device }}  # Path to the watchdog device
  safety_margin: 5

tags:
{% if patroni_tags is defined and patroni_tags | length > 0 %}
  {{ patroni_tags | replace(" ", "") | replace("=", ": ") | replace(",", "\n  ") }}
{% endif %}
{% set normalized_tags = patroni_tags | default('') | replace(" ", "") %}
{% if 'nosync=' not in normalized_tags and 'sync_priority=' not in normalized_tags %}
  nosync: false
{% endif %}
{% if 'noloadbalance=' not in normalized_tags %}
  noloadbalance: false
{% endif %}
{% if 'nofailover=' not in normalized_tags %}
  nofailover: false
{% endif %}
{% if 'clonefrom=' not in normalized_tags %}
  clonefrom: false
{% endif %}
{% if 'replicatefrom=' not in normalized_tags and patroni_replicatefrom | default('') | length > 0 %}
  replicatefrom: {{ patroni_replicatefrom | default('') }}
{% endif %}
